/*
 * Copyright (c) 2013-2015 SimaStudio. All rights reserved.
 *
 * This software is the confidential and proprietary information of SimaStudio.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with SimaStudio.
 *
 * SimaStudio MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * SimaStudio SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.asiainfo.integration.tibco;

import com.asiainfo.integration.controller.ScheduleJobController;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by qikai on 4/28/2016.
 */
public class EPCTester {

    private static final Logger LOGGER = LoggerFactory.getLogger(EPCTester.class);

    public static void main(String[] args) {

        String reqXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\"><soapenv:Body><ns1:exchangeResponse xmlns:ns1=\"http://epc.inter.upc.ailk.com\"><ns1:exchangeReturn xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"soapenc:string\">&lt;![CDATA[&lt;ContractRoot&gt;\n" +
                "  &lt;ResponseHead&gt;\n" +
                "    &lt;RspTransactionID&gt;30079&lt;/RspTransactionID&gt;\n" +
                "    &lt;RspTime&gt;20160428193202&lt;/RspTime&gt;\n" +
                "    &lt;RspType&gt;0&lt;/RspType&gt;\n" +
                "    &lt;RspCode&gt;0000&lt;/RspCode&gt;\n" +
                "    &lt;RspDesc&gt;Success.&lt;/RspDesc&gt;\n" +
                "  &lt;/ResponseHead&gt;\n" +
                "&lt;/ContractRoot&gt;]]&gt;</ns1:exchangeReturn></ns1:exchangeResponse></soapenv:Body></soapenv:Envelope>";
        Document srcDoc = null;
        try {
            srcDoc = DocumentHelper.parseText(reqXml);
        } catch (Exception e) {
            LOGGER.error("load reqXml error", e);
        }
        String doc = null;
        Document reqDoc = null;
        try {
            doc = srcDoc.selectSingleNode("//*[local-name()='exchangeReturn']").getText();
            LOGGER.debug(doc);
            reqDoc = DocumentHelper.parseText(doc);

            String rspType = reqDoc.selectSingleNode("//ContractRoot/ResponseHead/RspType").getText();
            String rspCode = reqDoc.selectSingleNode("//ContractRoot/ResponseHead/RspCode").getText();
            String rspDesc = reqDoc.selectSingleNode("//ContractRoot/ResponseHead/RspDesc").getText();

            String correlationId = reqDoc.selectSingleNode("//ContractRoot/ResponseHead/RspTransactionID").getText();
            String viewName = "exchangeReturn";
            String responseGeneratedDateTime = "TIME TO BE SET";

            StringBuffer buffer = new StringBuffer();
            buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            buffer.append("<ns0:EnterpriseProductCatalogResponse xmlns:ns0=\"http://www.tmforum.org/xml/tip/BusEnt/ProdResp\">");
            buffer.append("<ns0:Header>");
            buffer.append("<ns0:correlationId>");
            buffer.append(correlationId);
            buffer.append("</ns0:correlationId>");
            buffer.append("<ns0:responseGeneratedDateTime>");
            buffer.append(responseGeneratedDateTime);
            buffer.append("</ns0:responseGeneratedDateTime>");
            buffer.append("<ns0:viewName>");
            buffer.append(viewName);
            buffer.append("</ns0:viewName>");
            buffer.append("<ns0:systemName>UPC</ns0:systemName>");
            buffer.append("</ns0:Header>");
            buffer.append("<ns0:Body>");

            if("0".equals(rspType)) {
                buffer.append("<ns0:status>1</ns0:status>");
            } else {
                buffer.append("<ns0:status>0</ns0:status>");
                buffer.append("<ns0:failureDetails><ns0:failureDetail>");
                buffer.append("<ns0:errorCode>");
                buffer.append(rspCode);
                buffer.append("</ns0:errorCode>");
                buffer.append("<ns0:errorName>");
                buffer.append(rspType);
                buffer.append("</ns0:errorName>");
                buffer.append("<ns0:errorDescription>");
                buffer.append(rspDesc);
                buffer.append("</ns0:errorDescription>");
                buffer.append("</ns0:failureDetail></ns0:failureDetails>");
            }
            buffer.append("</ns0:Body>");
            buffer.append("</ns0:EnterpriseProductCatalogResponse>");
            LOGGER.debug("CHARKEY:"+buffer.toString());
        } catch(Exception e) {
            LOGGER.debug("The filed is \"doc\" null.");
            doc = "";
        }


    }

}